// pages/applyRepair/failInfo.js
const app = getApp();
Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '未解决',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",
    navs:[
      {title: "首页",class:"", path:"/pages/index/index", icon: "", font: "fa fa-home"},
      {title: "信息",class:"", path:"", icon: "", font: "fa fa-comments-o"},
      {title: "维修",class:"", path:"", icon: "", font: "fa fa-wrench"},
      {title: "我的",class:"co", path:"/pages/mine/mine", icon: "", font: "fa fa-user-circle"},
    ],
    starts: [true, false,false,false,false,],
    binds: [],
    array: [],
    images: [{url:''},],
    index: 0,
    positions:[],
    position: 0,
    start: "2022-02-02",
    date: "2022-02-02",
    time: "09:00",
    order_id: 0,
    remark: "",
    type: 0,
    title: "",
    code: "",
    addreess: "",
    tel: "",
    bank: "",
    bank_code: "",
    name: "",
    phone: "",
    email: "",
    orderList:[],
    hasChange: false,
    enableRequest: true,
    ready: false
  },
  onLoad: function (options) {
  },
  onReady: function () {
  },
  onShow: function () {
    let v = this;
    let today = app.ymd();
    let data = {};
    data.start = today;
    data.date = today;
    console.log("onShow",data);
    v.setData(data);
    v.getMyBinds();
  },
  getMyBinds: function() {
    let v = this;
    let token = app.globalData.token;
    if(!token) {
      app.afterLogin(v,v.getMyBinds);
      return;
    }
    let url = app.globalData.host + '/api/user/owner_list';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data.list || [];
          let addrs = [];
          for(let k in l) {
            let addr = l[k].xiaoqu + l[k].addr;
            l[k].address = addr;
            addrs.push(addr);
          }
          console.log("getMyBinds",l);
          v.setData({binds: l,array: addrs});
          // v.getPosition();
        }
      }
    })
  },
  getPosition() {
    let v = this;
    let url = app.globalData.host + '/api/common/fix_position';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data || [];
          console.log("position",l);
          v.setData({positions: l});
        }
      }
    })
  },
  bindDateChange(ev){
    this.setData({date: (ev.detail.value) || 0});
  },
  bindTimeChange(ev){
    this.setData({time: (ev.detail.value) || 0});
  },
  bindPosChange(ev){
    this.setData({position: (ev.detail.value) || 0});
  },
  bindKeyInput(ev){
    this.setData({title: ev.detail.value});
  },
  bindKeyInputPos(ev){
    this.setData({location: ev.detail.value});
  },
  bindPickerChange: function(ev) {
    this.setData({ index: ev.detail.value })
  },
  bindKeyInputName(ev){
    this.setData({name: ev.detail.value});
  },
  bindKeyInputPhone(ev){
    this.setData({phone: ev.detail.value});
  },
  bindKeyInputRemark(ev){
    this.setData({remark: ev.detail.value});
  },
  doRequestFail(ev){
    let v = this;
    let info = app.globalData.repairInfo;
    console.log("doRequestFail");
    let url = app.globalData.host + '/api/fix/status';
    let data = {token: app.globalData.token};
    let imgs = [];
    for(let k in v.data.images) if (v.data.images[k].url) imgs.push(v.data.images[k].url);
    data.content  = v.data.remark;
    data.imgs     = imgs.length ? imgs.toString() : '';
    data.fix_id   = info.fix_id;
    data.status   = -1;
    
    wx.request({
      url: url, data: data,
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          wx.showToast({ title: r.msg || '成功',icon:'none'})
          v.setData({enableRequest: false});
          setTimeout(function(){
            wx.navigateBack({ delta: 1,  });
          },2500);
          
        } else {
          wx.showToast({ title: r.msg || '失败',icon:'none'})
        }
      }
    })
  },
  setStartNum(ev) {
    const v = this;
    console.log("setStartNum",ev);
    let num = 0;
    let ds = ev.target.dataset;
    if (ds) num = parseInt(ds.value) || 0;
    let starts = v.data.starts;
    for(let k in starts) starts[k] = (k < num + 1 );
    v.setData({starts:starts});
  },
  addAvator(){
    let v = this;
    let images = v.data.images;
    let item = {url: ''};
    if (images.length > 2) {
      wx.showToast({ title: '最多添加三张图片', icon: 'none' });
      return;
    }
    images.push(item);
    v.setData({images:images});
  },
  uploadAvator(ev) {
    const v = this;
    console.log("uploadAvator",ev);
    let index = 0;
    let ds = ev.target.dataset;
    if (ds) index = parseInt(ds.index) || 0;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      sizeType: ['compressed'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        const size = res.tempFiles[0].size;
        const name = v.getRandFileName(tempFilePath);
        console.log(name,res);
        const url = app.globalData.host + "/api/common/upimg";
        wx.uploadFile({
          url: url, //仅为示例，非真实的接口地址
          filePath: tempFilePath,
          name: 'file',
          formData: {'user': v.data.uid },
          success (res){
            let data = res.data
            if(typeof data == 'string') data = JSON.parse(data);
            console.log(data,res);
            let images = v.data.images;
            if (data.code == 200) {
              let src = data.data.src;
              images[index].url = src;
              v.setData({images: images});
            }
          },
          fail (err) {
            console.log("fail", err);
          }
        })
      }
    })
  },
  getRandFileName(filePath) {
    let v = this;
    const extIndex = filePath.lastIndexOf('.');
    const extName = extIndex === -1 ? '' : filePath.substr(extIndex);
    let rname = parseInt(`${Date.now()}${Math.floor(Math.random() * 900 + 100)}`, 10).toString(36);
    let saveName = rname + extName;
    if(v.data.uid) {
      saveName = "" + v.data.uid + extName;
    }
    return saveName;
  },
  onSuccess: function (res) {
    console.log(res.detail);
  },
  onFail: function (res) {
    console.log(res);
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },

  onShareAppMessage: function () {
  }
})